<?php

/**
 * 分包任务类
 */

namespace App\Jobs;

use App\Repository\UploadExcelRepository;
use App\Repository\UpThree\MainOrderTwoRepostition;
use Carbon\Carbon;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

//use Log;


class TaskUploadExcel extends Job
{
    protected $willUpdateId;
    protected $type;
    protected $extraData;
    public $tries = 3;
    public $timeout = 300;
    public $log = null;

    public function __construct($type, $willUpdateId = 0, $extraData = [])
    {
        $this->log = new Logger('TaskUploadExcel');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/TaskUploadExcel.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );

        $this->willUpdateId = $willUpdateId;
        $this->type = $type;
//        传递的额外参数,不能带有文件对象
        $this->extraData = $extraData;
    }

    /**
     * 执行任务
     *
     * @return void
     */
    public function handle()
    {
        ini_set('max_execution_time', 800);
        switch ($this->type) {
            case 'CallTask':
                $Pository = new UploadExcelRepository();
                $Pository->handleCallTaskData($this->extraData);
                break;
            case 'RequestLogistics':
                $model = new MainOrderTwoRepostition();
                $model->expressTrack($this->extraData['express_company'], $this->extraData['express_number']);
                break;
            default:
                break;
        }

    }

    public function retryUntil()
    {
        //任务超时时间
        $time = 60 * 60;
        return Carbon::now()->addSeconds($time);
    }

    /**
     * 处理失败任务
     *
     * @return void
     */
    public function failed()
    {
        $this->log->info('操作文件失败' . $this->willUpdateId . '类型' . $this->type);
    }
}
